package com.baizhi.springbootshiro.service.impl;

import com.baizhi.springbootshiro.entity.User;
import com.baizhi.springbootshiro.mapper.UserMapper;
import com.baizhi.springbootshiro.service.RoleService;
import com.baizhi.springbootshiro.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chenzy
 * @since 2020-10-06
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    RoleService roleService;

    @Override
    public boolean save(User user) {
        String pwd = user.getPwd();
        String salt = RandomStringUtils.randomAlphanumeric(12);
        user.setSalt(salt);
        Md5Hash md5Hash = new Md5Hash(pwd, salt, 1024);
        user.setPwd(md5Hash.toHex());
        int insert = baseMapper.insert(user);
        md5Hash = null;
        return insert > 0;
    }

    @Override
    public boolean login(User user) {
        return false;
    }

    @Override
    public boolean logout(User user) {
        return false;
    }
}
